package com.shendu.task.service;

import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shendu.task.bo.DepartBo;
import com.shendu.task.bo.UserBo;
import com.shendu.task.domain.DepartDoc;
import com.shendu.task.domain.RetVo;
import com.shendu.task.domain.UserDoc;

@Service
public class UserService {
	@Autowired
	private UserBo userBo;
	@Autowired
	private DepartBo departBo;

	/**
	 * 用户登录或者注册<br>
	 * 如果用户没有注册，则注册，如果已经注册则登录
	 * 
	 * @param user
	 * @return 
	 */
	public RetVo login(UserDoc user) {
		UserDoc userDb = userBo.getByName(user.getName());
		if(userDb==null) {
			user.setUserId(UUID.randomUUID().toString());
			userBo.save(user);
			DepartDoc depart = departBo.getByNo(user.getDepartNo());
			depart.setUserCount(depart.getUserCount()+1);
			departBo.updateDepart(depart);
			return new RetVo(user,RetVo.SUCCESS,"注册并登录成功");
		}else {
			if(userDb.getPwd().equals(user.getPwd())) {
				return new RetVo(userDb);
			}
			return new RetVo(null,RetVo.FAIL,"密码错误");
		}
	}

	public List<UserDoc> list(UserDoc user) {
		return userBo.list(user);
	}

	public UserDoc checkUser(UserDoc user) {
		return userBo.getByName(user.getName());
	}
}
